//
//  SYHomeCell.swift
//  SYWeibo
//
//  Created by Saya on 16/4/12.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
import SnapKit

class SYHomeCell: UITableViewCell {
    private var tempConstain:Constraint?
    
    var statusModel:SYCellStatusViewModel?{
        didSet{
            originWioboView.statusModel = statusModel
            toolView.statusModel = statusModel
            
            //通过数据判断是否有转发微博的字段,因为纯日志是没有该字段的,来设置底部工具条的top,这样就可以解决中间空一块的bug了
            //先将需要update的约束删除,这样在修改的时候就不会出现约束重复的问题了
            self.tempConstain?.uninstall()
            if statusModel?.status?.retweeted_status == nil{
                //纯日志
                //修改约束
                toolView.snp_updateConstraints(closure: { (make) -> Void in
                    self.tempConstain = make.top.equalTo(originWioboView.snp_bottom).constraint
                })
                //隐藏转发view
                retweetView.hidden = true
                //将转发的数据设置nil
                retweetView.statusModel = nil
            }else{
                //转发日志
                //修改约束
                toolView.snp_makeConstraints(closure: { (make) -> Void in
                    self.tempConstain = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                //显示转发view
                retweetView.hidden = false
                //设置数据
                retweetView.statusModel = statusModel
            }
        }
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI (){
        //添加子控件
        contentView.addSubview(originWioboView)
        contentView.addSubview(toolView)
        contentView.addSubview(retweetView)

        
        originWioboView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            //为了测试看见效果
//            make.height.equalTo(100)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originWioboView.snp_bottom).offset(marginForCell)
            make.left.right.equalTo(contentView)
        }
        
        toolView.snp_makeConstraints { (make) -> Void in
            self.tempConstain = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolView.snp_bottom)
            make.left.top.right.equalTo(self)
        }
        

    }
    
    lazy var originWioboView:SYOriginWioboView = SYOriginWioboView()
    lazy var toolView:SYToolView = SYToolView()
    lazy var retweetView:SYRetweetView = SYRetweetView()
}
